/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.lucava.boatskeeper.server;

import org.lucava.boatskeeper.server.handler.boat.AddBoatHandler;
import org.lucava.boatskeeper.server.handler.boat.DeleteBoatsHandler;
import org.lucava.boatskeeper.server.handler.boat.GetBoatHandler;
import org.lucava.boatskeeper.server.handler.boat.GetBoatsHandler;
import org.lucava.boatskeeper.server.handler.boat.GetCategoriesHandler;
import org.lucava.boatskeeper.server.handler.boat.SaveBoatHandler;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import net.customware.gwt.dispatch.client.service.DispatchService;
import net.customware.gwt.dispatch.server.ActionHandlerRegistry;
import net.customware.gwt.dispatch.server.DefaultActionHandlerRegistry;
import net.customware.gwt.dispatch.server.DefaultDispatch;
import net.customware.gwt.dispatch.server.Dispatch;
import net.customware.gwt.dispatch.shared.Action;
import net.customware.gwt.dispatch.shared.ActionException;
import net.customware.gwt.dispatch.shared.Result;
import org.lucava.boatskeeper.server.handler.user.ConnectUserHandler;

/**
 *
 * @author glenn
 */
public class SimpleDispatchServlet extends RemoteServiceServlet implements DispatchService {

    private Dispatch dispatch;

    public SimpleDispatchServlet() {

        ActionHandlerRegistry registry = new DefaultActionHandlerRegistry();
        registry.addHandler(new AddBoatHandler());
        registry.addHandler(new DeleteBoatsHandler());
        registry.addHandler(new GetBoatHandler());
        registry.addHandler(new GetBoatsHandler());
        registry.addHandler(new SaveBoatHandler());
        registry.addHandler(new GetCategoriesHandler());
        registry.addHandler(new ConnectUserHandler());
        dispatch = new DefaultDispatch(registry);

    }

    @Override
    public Result execute(Action<?> action) throws ActionException {
        try {
            return dispatch.execute(action);
        } catch (RuntimeException e) {
            log("Exception while executing " + action.getClass().getName() + ": " + e.getMessage(), e);
            throw e;
        }
    }
}